
#include "mainwindow.h"
#include "config.h"
#include <QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<QTimer>
#include<ctime>
#include <QAudioOutput>
#include <iostream>
#include <QDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)

{
    initMusic();
    initScene();
    playGame();
}

MainWindow::~MainWindow()
{

}

 void MainWindow::initScene()
{

     setFixedSize(GAME_WINDTH,GAME_HEIGHT);//设置窗口
    setWindowTitle(GAME_TITLE);//设置标题
     setWindowIcon(QIcon(":/ph/images/title.png"));//加载图标

    m_Timer.setInterval(GAME_RATE);//

     m_recorder=0;
//    m_recordera=0;
//     m_recorderb=0;

     srand((unsigned int)time(NULL));
}

void MainWindow::initMusic()
{if(flag==1&&flagw==1)
     {
     QObject::connect(&winTimer,SIGNAL(timeout()),this,SLOT(win()));
     winTimer.start(120000);
     }
     QObject::connect(&hurtTimer,SIGNAL(timeout()),this,SLOT(setHurt()));
}

void MainWindow::playGame()
{
    m_Timer.start();
    enemystart();
    weaponstart();
    if(m_planes.mhp<=0&&flag==1&&flagw==1)
    {
        flag=0;
        MainWindow::defeat() ;
    }
    connect(&m_Timer,&QTimer::timeout,[=]()
    {
                enemystart();
                weaponstart();
                updatePosition();
                update();
                collision_e_b();
                collision_m_w1();
                collision_m_w2();
                collision_m_e1();
                collision_m_e2();
                collision_m_e3();
                collision_m_e47();

    }
            );

}

void MainWindow::updatePosition()
{
    mHp.move(GAME_WINDTH-110,0);
    mHp.setText(QString("Hp:")+QString::number(m_planes.mhp));
    mHp.resize(110,65);
    mHp.setParent(this);
    mHp.setStyleSheet("font-size:25px;border:5px double #ddd;color:#ddd;font-weight:bold;");
    mHp.show();
        //更新坐标
        m_map.maPosition();

        //测试子弹
       // temp_bullet.m_free=false;
        //temp_bullet.updatePosition();
        m_host.shoot();
//        m_host.shoota();
//        m_host.shootb();

        //fei kong xian zi dan dang qian zuo biao
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(m_host.m_bullets[i].m_free==false)
            {
                m_host.m_bullets[i].updatePosition();
            }
        }
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(m_host.m_bullets[i].m_freea==false)
            {
                m_host.m_bullets[i].updatePosition();
            }
        }
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(m_host.m_bullets[i].m_freeb==false)
            {
                m_host.m_bullets[i].updatePosition();
            }
        }

        //敌机出场
        for(int i=0;i<ENEMY_NUM;i++)
        {
            if(m_enemys[i].m_free==false||m_enemys[i].m_free2==false||m_enemys[i].m_free3==false||m_enemys[i].m_free4==false
                ||m_enemys[i].m_free5==false)
            {
                m_enemys[i].updatePosition();
            }
        }
        //军火出场
        for(int i=0;i<WEAPON_NUM;i++)
        {
            if(m_weapons[i].m_free4==false||m_weapons[i].m_free5==false)
            {
                m_weapons[i].updatePosition();
            }
        }
}

void MainWindow::paintEvent(QPaintEvent *)

{
        QPainter painter(this);
        painter.drawPixmap(0,m_map.m_map1_Y,m_map.m_map1) ;
         painter.drawPixmap(0,m_map.m_map2_Y,m_map.m_map2) ;

        painter.drawPixmap(m_host.m_x,m_host.m_y,m_host.m_plane);


         for(int i=0;i<BULLET_NUM;i++)
        {
            if(m_host.m_bullets[i].m_free==false)
            {
        painter.drawPixmap(m_host.m_bullets[i].m_x,m_host.m_bullets[i].m_y,m_host.m_bullets[i].m_bullet1);
            }
         }

         for(int i=0;i<BULLET_NUM;i++)
         {
            if(m_host.m_bullets[i].m_freea==false)
            {
        painter.drawPixmap(m_host.m_bullets[i].m_xa,m_host.m_bullets[i].m_ya,m_host.m_bullets[i].m_bullet2);
            }
         }
         for(int i=0;i<BULLET_NUM;i++)
         {
            if(m_host.m_bullets[i].m_freeb==false)
            {
        painter.drawPixmap(m_host.m_bullets[i].m_xb,m_host.m_bullets[i].m_yb,m_host.m_bullets[i].m_bullet3);
            }
         }


         for(int i=0;i<ENEMY_NUM;i++)
         {
            if(m_enemys[i].m_free==false)
            {
        painter.drawPixmap(m_enemys[i].m_x,m_enemys[i].m_y,m_enemys[i].m_enemy1);
            }
            if(m_enemys[i].m_free2==false)
            {
        painter.drawPixmap(m_enemys[i].m_x2,m_enemys[i].m_y2,m_enemys[i].m_enemy2);
            }
            if(m_enemys[i].m_free3==false)
            {
        painter.drawPixmap(m_enemys[i].m_x3,m_enemys[i].m_y3,m_enemys[i].m_enemy3);
            }
            if(m_enemys[i].m_free4==false)
            {
        painter.drawPixmap(m_enemys[i].m_x4,m_enemys[i].m_y4,m_enemys[i].m_enemy4);
            }
            if(m_enemys[i].m_free5==false)
            {
        painter.drawPixmap(m_enemys[i].m_x5,m_enemys[i].m_y5,m_enemys[i].m_enemy5);
            }

         }
         for(int i=0;i<WEAPON_NUM;i++)
         {
            if(m_weapons[i].m_free4==false)
            {
        painter.drawPixmap(m_weapons[i].m_x4,m_weapons[i].m_y4,m_weapons[i].m_weapon1);
            }
            if(m_weapons[i].m_free5==false)
            {
        painter.drawPixmap(m_weapons[i].m_x5,m_weapons[i].m_y5,m_weapons[i].m_weapon2);
            }
         }
}

void MainWindow::mouseMoveEvent(QMouseEvent * event)
{

        //鼠标移动
        int x  = event->x()-m_host.m_rect.width()*0.5;
        int y = event ->y()-m_host.m_rect.height()*0.5;
        //边界检测
        if(x<=0-m_host.m_rect.width()*0.5)
        {
            x=0-m_host.m_rect.width()*0.5;
            if(x>=GAME_WINDTH-m_host.m_rect.width())
            {
                x=GAME_WINDTH-m_host.m_rect.width();
            }
            if(y>=GAME_HEIGHT-m_host.m_rect.height())
            {
                y=GAME_HEIGHT-m_host.m_rect.height();
            }
        }
        if(x>=GAME_WINDTH-m_host.m_rect.width()*0.5)
        {
            x=GAME_WINDTH-m_host.m_rect.width()*0.5;
            if(x>=GAME_WINDTH-m_host.m_rect.width()*0.5)
            {
                x=GAME_WINDTH-m_host.m_rect.width()*0.5;
            }
            if(y>=GAME_HEIGHT-m_host.m_rect.height())
            {
                y=GAME_HEIGHT-m_host.m_rect.height();
            }
        }

        m_host.setPosition(x,y);
}
//void MainWindow::keyPressEvent(QKeyEvent * event)
//{

         //鼠标移动
//         int x  = event->m_host.-m_host.m_rect.width()*0.5;
//         int y = event ->pos.y()-m_host.m_rect.height()*0.5;
//         if(event->key() == Qt::Key_Left){//左
//            x->label->move(x-10,y);
//         }
//         else if(event->key() == Qt::Key_Right){//右
//            x->label->move(x+10,y);
//         }
//         else if(event->key() == Qt::Key_Up){//上
//            y->label->move(x,y-10);
//         }
//         else if(event->key() == Qt::Key_Down){//下
//            y->label->move(x,y+10);
//         }



//         //边界检测
//         if(x<=0-m_host.m_rect.width()*0.5)
//         {
//            x=0-m_host.m_rect.width()*0.5;
//            if(x>=GAME_WINDTH-m_host.m_rect.width())
//            {
//        x=GAME_WINDTH-m_host.m_rect.width();
//            }
//            if(y>=GAME_HEIGHT-m_host.m_rect.height())
//            {
//        y=GAME_HEIGHT-m_host.m_rect.height();
//            }
//         }
//         if(x>=GAME_WINDTH-m_host.m_rect.width()*0.5)
//         {
//            x=GAME_WINDTH-m_host.m_rect.width()*0.5;
//            if(x>=GAME_WINDTH-m_host.m_rect.width()*0.5)
//            {
//        x=GAME_WINDTH-m_host.m_rect.width()*0.5;
//            }
//            if(y>=GAME_HEIGHT-m_host.m_rect.height())
//            {
//        y=GAME_HEIGHT-m_host.m_rect.height();
//            }
//         }

//         m_host.setPosition(x,y);
//}

void MainWindow::enemystart()
{
        m_recorder++;
        if(m_recorder<ENEMY_TIMEX)
        {
            return;
        }
        m_recorder=0;

        for(int i=0;i<ENEMY_NUM;i++)
        {
            if(m_enemys[i].m_free)
            {
                m_enemys[i].m_free=false;

                m_enemys[i].m_y=0;
                m_enemys[i].m_x=rand()%(GAME_WINDTH-m_enemys[i].m_rect1.width());
                break;
            }
        }
             for(int i=0;i<ENEMY_NUM;i++)
        {
            if(m_enemys[i].m_free2)
            {
                m_enemys[i].m_free2=false;

                m_enemys[i].m_y2=0;
                m_enemys[i].m_x2=rand()%(GAME_WINDTH-m_enemys[i].m_rect2.width());
                break;
            }
        }
              for(int i=0;i<ENEMY_NUM;i++)
        {
            if(m_enemys[i].m_free3)
            {
                m_enemys[i].m_free3=false;

                m_enemys[i].m_y3=0;
                m_enemys[i].m_x3=rand()%(GAME_WINDTH-m_enemys[i].m_rect3.width());
                break;
            }
        }
              for(int i=0;i<ENEMY_NUM;i++)
              {
            if(m_enemys[i].m_free4)
            {
                m_enemys[i].m_free4=false;

                m_enemys[i].m_y4=0;
                m_enemys[i].m_x4=rand()%(GAME_WINDTH-m_enemys[i].m_rect4.width());
                break;
            }
              }
              for(int i=0;i<ENEMY_NUM;i++)
              {
            if(m_enemys[i].m_free5)
            {
                m_enemys[i].m_free5=false;

                m_enemys[i].m_y5=0;
                m_enemys[i].m_x5=rand()%(GAME_WINDTH-m_enemys[i].m_rect5.width());
                break;
            }
              }

}

void MainWindow::weaponstart()
{
        m_recorder++;
        if(m_recorder<WEAPON_TIMEX)
        {
            return;
        }
        m_recorder=0;

        for(int i=0;i<WEAPON_NUM;i++)
        {
            if(m_weapons[i].m_free4)
            {
                m_weapons[i].m_free4=false;

                m_weapons[i].m_y4=0;
                m_weapons[i].m_x4=rand()%(GAME_WINDTH-m_weapons[i].m_rect4.width());
                break;
            }
        }
        for(int i=0;i<WEAPON_NUM;i++)
        {
            if(m_weapons[i].m_free5)
            {
                m_weapons[i].m_free5=false;

                m_weapons[i].m_y5=0;
                m_weapons[i].m_x5=rand()%(GAME_WINDTH-m_weapons[i].m_rect5.width());
                break;
            }
        }
}

void MainWindow::collision_e_b()
{
        for(int i=0;i<ENEMY_NUM;i++)
        {
            if(m_enemys[i].m_free)
            {
                continue;
            }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_free)
            {
                continue;
            }

        if(m_enemys[i].m_rect1.intersects(m_host.m_bullets[j].m_rect))
            {

                m_host.m_bullets[j].m_free=true;
                m_enemys[i].e_hp1--;
                if(m_enemys[i].e_hp1==0)
                {
                    m_enemys[i].m_free=true;
                    m_enemys[i].e_hp1=5;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_freea)
            {
                continue;
            }

            if(m_enemys[i].m_rect1.intersects(m_host.m_bullets[j].m_recta))
            {
                m_host.m_bullets[j].m_free=true;
                m_enemys[i].e_hp1--;
                if(m_enemys[i].e_hp1==0)
                {
                    m_enemys[i].m_free=true;
                    m_enemys[i].e_hp1=5;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_freeb)
            {
                continue;
            }

            if(m_enemys[i].m_rect1.intersects(m_host.m_bullets[j].m_rect))
            {
                m_host.m_bullets[j].m_free=true;
                m_enemys[i].e_hp1--;
                if(m_enemys[i].e_hp1==0)
                {
                    m_enemys[i].m_free=true;
                    m_enemys[i].e_hp1=5;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free2)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_free)
            {
                continue;
            }

            if(m_enemys[i].m_rect2.intersects(m_host.m_bullets[j].m_rect))
            {

                m_host.m_bullets[j].m_free=true;
                m_enemys[i].e_hp2--;
                if(m_enemys[i].e_hp2==0)
                {
                    m_enemys[i].m_free2=true;
                    m_enemys[i].e_hp2=15;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free2)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_freea)
            {
                continue;
            }

            if(m_enemys[i].m_rect2.intersects(m_host.m_bullets[j].m_recta))
            {

                m_host.m_bullets[j].m_freea=true;
                m_enemys[i].e_hp2--;
                if(m_enemys[i].e_hp2==0)
                {
                    m_enemys[i].m_free2=true;
                    m_enemys[i].e_hp2=15;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free2)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_freeb)
            {
                continue;
            }

            if(m_enemys[i].m_rect2.intersects(m_host.m_bullets[j].m_rectb))
            {

                m_host.m_bullets[j].m_freeb=true;
                m_enemys[i].e_hp2--;
                if(m_enemys[i].e_hp2==0)
                {
                    m_enemys[i].m_free2=true;
                    m_enemys[i].e_hp2=15;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free3)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_free)
            {
                continue;
            }

            if(m_enemys[i].m_rect3.intersects(m_host.m_bullets[j].m_rect))
            {

                m_host.m_bullets[j].m_free=true;
                m_enemys[i].e_hp3--;
                if(m_enemys[i].e_hp3==0)
                {
                    m_enemys[i].m_free3=true;
                     m_enemys[i]. e_hp3=10;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free3)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_freea)
            {
                continue;
            }

            if(m_enemys[i].m_rect3.intersects(m_host.m_bullets[j].m_recta))
            {

                m_host.m_bullets[j].m_freea=true;
                m_enemys[i].e_hp3--;
                if(m_enemys[i].e_hp3==0)
                {
                    m_enemys[i].m_free3=true;
                     m_enemys[i]. e_hp3=10;
                }
            }
        }}
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_free3)
        {
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_host.m_bullets[j].m_freeb)
            {
                continue;
            }

            if(m_enemys[i].m_rect3.intersects(m_host.m_bullets[j].m_rectb))
            {

                m_host.m_bullets[j].m_freeb=true;
                m_enemys[i].e_hp3--;
                if(m_enemys[i].e_hp3==0)
                {
                    m_enemys[i].m_free3=true;
                   m_enemys[i]. e_hp3=10;
                }
            }
        }}
}

void MainWindow::collision_m_w1()
{

        for(int i=0;i<WEAPON_NUM;i++)
        {
        if(m_planes.m_rect.intersects(m_weapons[i].m_rect4))
        {
            m_weapons[i].m_free4=true;
            for(int j=0;j<ENEMY_NUM;j++)
            {
                m_enemys[j].m_free4=true;
                m_enemys[j].m_free5=true;
            }
        }
        }
}

void MainWindow::collision_m_w2()
{

        for(int i=0;i<WEAPON_NUM;i++)
        {
        if(m_planes.m_rect.intersects(m_weapons[i].m_rect5))
        {
             m_weapons[i].m_free5=true;
             for(int j=0;j<ENEMY_NUM;j++)
             {
                m_enemys[j].m_free=true;
                m_enemys[j].m_free2=true;
                m_enemys[j].m_free3=true;
             }
        }
        }
}

void MainWindow::collision_m_e1()
{
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_planes.m_rect.intersects(m_enemys[i].m_rect1))
        {
             m_enemys[i].m_free=true;

             if(hurtable){
             m_planes.mhp-=1;
             hurtable=0;
             hurtTimer.start(2000);
             }
             if(m_planes.mhp<=0)
             {
                if(m_planes.mhp<=0&&flag==1)
                {
                   flag=0;
                   MainWindow::defeat() ;
                    m_planes.mhp=0;
                }
             }
             break;
        }
        }
}

void MainWindow::collision_m_e2()
{
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_planes.m_rect.intersects(m_enemys[i].m_rect2))
        {
             m_enemys[i].m_free2=true;
             if(hurtable){
                m_planes.mhp-=2;
                hurtable=0;
                hurtTimer.start(2000);
             }
             if(m_planes.mhp<=0)
             {
                if(m_planes.mhp<=0&&flag==1)
                {
                   flag=0;
                   MainWindow::defeat() ;
                    m_planes.mhp=0;
                }
             }
             break;
        }
        }
}

void MainWindow::collision_m_e3()
{
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_planes.m_rect.intersects(m_enemys[i].m_rect3))
        {
             m_enemys[i].m_free3=true;
             if(hurtable){
                m_planes.mhp-=2;
                hurtable=0;
                hurtTimer.start(2000);
             }
             if(m_planes.mhp<=0)
             {
                if(m_planes.mhp<=0&&flag==1)
                {
                   flag=0;
                   MainWindow::defeat() ;
                     m_planes.mhp=0;
                }
             }
             break;
        }
        }
}

void MainWindow::collision_m_e47()
{
        for(int i=0;i<ENEMY_NUM;i++)
        {
        if(m_planes.m_rect.intersects(m_enemys[i].m_rect4)||m_planes.m_rect.intersects(m_enemys[i].m_rect5))
        {
             if(m_planes.m_rect.intersects(m_enemys[i].m_rect5))
             m_enemys[i].m_free5=true;
             if(m_planes.m_rect.intersects(m_enemys[i].m_rect4))
             m_enemys[i].m_free4=true;
             if(hurtable){
             m_planes.mhp-=1;
             hurtable=0;
             hurtTimer.start(2000);
             }
             if(m_planes.mhp<=0&&flag==1)
             {
                flag=0;
                MainWindow::defeat() ;
                m_planes.mhp=0;
             }
             break;
        }
        }
}

void MainWindow::solve()
{

         updatePosition();
        update();
}

void MainWindow::win()
{

        flagw=0;
        QDialog *d=new QDialog;d->show();
        d->setStyleSheet(QString("background-image:url(")+GAME_WIN+QString(")"));
        winTimer.stop();

}

void MainWindow::setHurt()
{
        hurtable=1;
        hurtTimer.stop();
}
void MainWindow::defeat()
{
        flag=0;
        QDialog *p=new QDialog;p->show();
        p->setStyleSheet(QString("background-image:url(")+GAME_OVER+QString(")"));
}

